package com.ruoyi.web.controller.web.controller.sqlservice;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.sqlservice.domain.SysSql;
import com.ruoyi.web.controller.sqlservice.service.ISysSqlService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * SQL接口模板Controller
 *
 * @author Zliming
 * @date 2020-03-30
 */
@Controller
@RequestMapping("/sqlservice/sqledit")
public class SysSqlController extends BaseController {
    private String prefix = "sqlservice/sqledit";

    @Autowired
    private ISysSqlService sysSqlService;

    @RequiresPermissions("sqlservice:sqledit:view")
    @GetMapping()
    public String sqledit() {
        return prefix + "/sqledit";
    }

    /**
     * 查询SQL接口模板列表
     */
    @RequiresPermissions("sqlservice:sqledit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysSql sysSql) {
        startPage();
        List<SysSql> list = sysSqlService.selectSysSqlList(sysSql);
        return getDataTable(list);
    }

    /**
     * 导出SQL接口模板列表
     */
    @RequiresPermissions("sqlservice:sqledit:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysSql sysSql) {
        List<SysSql> list = sysSqlService.selectSysSqlList(sysSql);
        ExcelUtil<SysSql> util = new ExcelUtil<SysSql>(SysSql.class);
        return util.exportExcel(list, "sqledit");
    }

    /**
     * 新增SQL接口模板
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存SQL接口模板
     */
    @RequiresPermissions("sqlservice:sqledit:add")
    @Log(title = "SQL接口模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysSql sysSql) {
        return toAjax(sysSqlService.insertSysSql(sysSql));
    }

    /**
     * 修改SQL接口模板
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SysSql sysSql = sysSqlService.selectSysSqlById(id);
        mmap.put("sysSql", sysSql);
        return prefix + "/edit";
    }

    /**
     * 修改保存SQL接口模板
     */
    @RequiresPermissions("sqlservice:sqledit:edit")
    @Log(title = "SQL接口模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysSql sysSql) {
        return toAjax(sysSqlService.updateSysSql(sysSql));
    }

    /**
     * 删除SQL接口模板
     */
    @RequiresPermissions("sqlservice:sqledit:remove")
    @Log(title = "SQL接口模板", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysSqlService.deleteSysSqlByIds(ids));
    }
}
